import * as qiniu from 'qiniu-js'
import axios from 'axios'

const util = {
    allInfoType : [{
        label: '文字',
        value: 'text'
    }, {
        label: '可选列表',
        value: 'enum'
    }, {
        label: '图片',
        value: 'image'
    }, {
        label: '日期',
        value: 'date'
    }, {
        label: '身份证',
        value: 'idcard'
    }, {
        label: '数字',
        value: 'number'
    }],
    getInfoBelongLabel(value) {
        var allInfoBelong = [{
            label: '基本信息',
            value: 'basic'
        }, {
            label: '专家信息',
            value: 'expert'
        }, {
            label: '证件信息',
            value: 'photo'
        }];
        for (let i = 0; i < allInfoBelong.length; i ++) {
            if (allInfoBelong[i].value === value)  {
                return allInfoBelong[i].label;
            }
        }
    },
    getInfoTypeValue (label) {
        var allInfoType = this.allInfoType;
        var res = label;
        for (let i = 0; i < allInfoType.length; i ++) {
            if (allInfoType[i].label === label) {
                res = allInfoType[i].value;
                break;
            }
        }
        return res;
    },
    getInfoTypeLabel (value) {
        var allInfoType = this.allInfoType;
        for (let i = 0; i < allInfoType.length; i++) {
            if (allInfoType[i].value === value) {
                return allInfoType[i].label;
            }
        }
    },
    getQuestionKey (label) {
        if (label === '是') {
            return 1;
        }
        else if (label === '否') {
            return 0;
        }
        return label;
    },
    uploadFile (base64String, that) {
        if (base64String.indexOf('http') !== -1) {
            return base64String;
        }
        //生成文件名字   随机10位字符串 + 时间戳
        const fileName = new Date().getTime() + Math.random().toString(36).substr(2);
        var blob = this.getBlob(base64String);
        axios.get('/index/QiNiu/getQiNiuToken')
            .then((datas) => {
                var token = datas.data.uptoken;
                var config = {
                    useCdnDomain: true,
                    region: qiniu.region.z0
                };
                var putExtra = {
                    fname: '',
                    params: {},
                    mimeType: null
                };
                let observable = qiniu.upload(blob, fileName, token, putExtra, config);
                observable.subscribe({
                    next (res) {
                        console.log(res)
                    },
                    error (err) {
                   
                    }
                })
              
            }, (err) => {
                
            });
            return that.$store.state.qiniuDomain + fileName;
         
    },
    getBlob (base64String) {
        var dataurl = base64String;
        var arr = dataurl.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = window.atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], { type: mime });
    },
    //根据参数获取路由
    getRoute(route) {
        
        var res = '';
        for (let i in route) {
            res += `${i}=${route[i]}&`
        }
        res = res.slice(0, res.length - 1);
        return res;

    }
}

export default util